//
//  MessageView.m
//  K-Music
//
//  Created by CHUN on 16/3/5.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "MessageView.h"
#import "Chat_Mode.h"

@implementation MessageView

-(id)initWithFrame:(CGRect)frame superViewCtr:(UIViewController *)superCtr{
    
    self = [super initWithFrame:frame];
    if(self){
        _superViewCtr = superCtr;
        
        _MeassgeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:(UITableViewStylePlain)];
        _MeassgeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _MeassgeTableView.allowsSelection = YES;
        _MeassgeTableView.delegate = self;
        _MeassgeTableView.dataSource = self;
        _MeassgeTableView.backgroundColor = RGB(255, 255, 255);
        
        [self addSubview:_MeassgeTableView];
        
        [self get_chat_group];
        
    }
    return self;
}

- (void)get_chat_group
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/chat_group?page=0&size=20&sort=createdDate,DESC"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功GET --> %@", dic);
        if ( [dic[@"errorCode"]intValue] != 1404 )
        {
            NSArray*content=dic[@"body"][@"content"];
            
            _chatData=[NSMutableArray array];
            
            for (NSDictionary * Data in content) {
                Chat_Mode * mode =[[Chat_Mode alloc]init];
                
                mode.address =Data[@"address"];
                mode.aid =Data[@"id"];
                mode.createdBy =Data[@"createdBy"];
                mode.createdDate =Data[@"createdDate"];
                mode.descriptionX =Data[@"description"];
                mode.lastModifiedBy =Data[@"lastModifiedBy"];
                mode.lastModifiedDate =Data[@"lastModifiedDate"];
                
                mode.latitude =Data[@"latitude"];
                mode.longitude =Data[@"longitude"];
                
                mode.level =Data[@"level"];
                mode.name =Data[@"name"];
                mode.pictureUrl =Data[@"pictureUrl"];
                mode.tag =Data[@"tag"];
                
                [mode fixedString];
                
                [_chatData addObject:mode];
                
            }
            [_MeassgeTableView reloadData];
        }
        else{
            _chatData = nil;
            [_MeassgeTableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
        [MBProgressHUD showErrorWithStatus:@"个人信息获取失败" toView:self];
        
    }];
}

#pragma mark ------- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH/4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _chatData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    // 先清空cell.contentView中的子view
    for (UIView * subview in cell.contentView.subviews)
    {
        [subview removeFromSuperview];
    }
    
    
    Chat_Mode * mode = [[Chat_Mode alloc]init];
    mode =_chatData[indexPath.row];
    
    
    // 图片
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH/4-20, WIDTH/4-20)];
    iconView.layer.cornerRadius = iconView.bounds.size.width * 0.5;
    iconView.layer.masksToBounds =YES;
    [iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",mode.pictureUrl]]];
    
    iconView.userInteractionEnabled = YES;
    UITapGestureRecognizer * PersonalTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Meassage_Pic)];
    [iconView addGestureRecognizer:PersonalTap];
    
    [cell.contentView addSubview:iconView];
    
    //标题
    UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconView.frame)+20, 10 , WIDTH/4,WIDTH/10)];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.text = mode.name;
    nameLabel.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:nameLabel];
    
    UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame), 10,WIDTH/2 ,WIDTH/10)];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.text = mode.createdDate;
    timeLabel.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:timeLabel];
    
    //正文
    UILabel *  contentLable = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconView.frame)+20, CGRectGetMaxY(nameLabel.frame) , WIDTH*0.7, WIDTH/8)];
    contentLable.textAlignment = NSTextAlignmentLeft;
    contentLable.text = mode.tag;
    
    contentLable.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:contentLable];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];
}

- (void)Meassage_Pic
{
    NSLog(@"Meassage_Pic");
    
}
@end
